new Vue({
    el: '#app',
    data(){
        return {
            doctors:[],
            activeIndex: '2',
            updateFormVisible : false , // 修改弹窗显示
            updateForm:{}, //修改列表
            formLabelWidth: '120px',
            searchByCondition:{}, // 条件查询
            addFormVisible: false,//添加显示
            addForm: {}, // 添加表单
            multipleSelection:[],//多选列表
            // 规则
            rules:{
            name: [
                { required: true, message: '请输入医生名称', trigger: 'blur' },
                ],
                id: [
                { required: true, message: '请输入患者身份证号', trigger: 'blur' },
                ],
                gender: [
                { required: true, message: '请选择性别', trigger: 'blur' },
                ],
                specialty: [
                    { required: true, message: '请选择专业', trigger: 'blur' },
                    ],
                department: [
                    { required: true, message: '请选择科室', trigger: 'blur' },
                    ],
            },

        }
    },
    methods: {
                // 导航
                handleSelect(key, keyPath) {
                    if(key==1){
                         location.href="patient.html"
                    }else if(key==3){
                        location.href="bill.html"
                    }else{
                        location.href="registration.html"
                    }
                   
                  },
        // 编辑
        handleEdit(index, row) {
            this.updateFormVisible=true
            // 数据回写
            this.updateForm=row
        },
                 // 添加医生信息
                 add() {
                    let _this = this;
                    console.log(this.addForm.name)
                    if(this.addForm.name==undefined|this.addForm.id==undefined|this.addForm.gender==undefined|this.addForm.specialty==undefined|this.addForm.department==undefined){
                        _this.$message({
                            message: "请填写完整信息",
                            type:"error"
                        })
                        return;
                    }
                    axios(
                        {
                            method:"POST",
                            url: "http://localhost/doctors/add",
                            data:_this.addForm
                        }
                    ).then(function(resp){
                        console.log(resp.data);
                       _this.getAllDoctors();
                       _this.addFormVisible=false;
                       if(resp.data.success){
                        _this.$message({
                            message: resp.data.message,
                            type:"success"
                        })
                       }else{
                        _this.$message({
                            message: resp.data.message,
                            type:"error"
                        })
                       }
        
                    }).catch(function(resp){
                        _this.$message({
                            message: "身份证格式错误",
                            type:"error"
                        })
                    })
                  },
                  // 条件查询
                  getByCondition(){
                    let _this = this;
                    axios(
                        {
                            method:"POST",
                            url: "http://localhost/doctors/get/condition",
                            data:_this.searchByCondition
                        }
                    ).then(function(resp){
                        console.log(resp.data);
                        _this.doctors=resp.data.data;
                    })
                }
                  ,
        // 删除
        handleDelete(index, row) {
            console.log(index, row);
            let _this = this;
            this.$confirm('此操作将永久删除该患者信息, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              axios(
                  {
                      method:"GET",
                      url: "http://localhost/doctors/delete/"+row.id,
                  }
              ).then(function(resp){
                  console.log(resp.data);
                 _this.getAllDoctors();
                 _this.$message({
                  message: resp.data.message,
                  type:"success"
              })
              })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消删除'
              });          
            });
  
        },
        handleSelectionChange(val) {
            this.multipleSelection=val
          },
         // 批量删除
         deletes(){
            let _this = this;
            let ids=[]
            _this.multipleSelection.forEach(item=>{
                ids.push(item.id)
            })
            console.log(ids)
            this.$confirm('此操作将永久删除该患者信息, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              axios(
                  {
                      method:"POST",
                      url: "http://localhost/doctors/delete/ids",
                      data: ids
                  }
              ).then(function(resp){
                _this.multipleSelection=[];
                  console.log(resp.data);
                 _this.getAllDoctors();
                 _this.$message({
                  message: resp.data.message,
                  type:"success"
              })
              })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消删除'
              });          
            });
  
        },
         // 修改患者信息
         update() {
            let _this = this;
            axios(
                {
                    method:"POST",
                    url: "http://localhost/doctors/update",
                    data:_this.updateForm
                }
            ).then(function(resp){
                console.log(resp.data);
               _this.getAllDoctors();
               _this.updateFormVisible=false;
               _this.$message({
                message: resp.data.message,
                type:"success"
            })
            })
          },


         // 根据医生id查询医生信息
         getDoctorById(id){
            this.dialogVisiblePatient=true
            console.log(id)
            let _this = this;
            axios(
                {
                    method:"GET",
                    url: "http://localhost/doctors/get/id/"+id,
                }
            ).then(function(resp){
                console.log(resp.data);
                _this.doctor=resp.data.data;
            })
         },


        // 获得所有医生
        getAllDoctors(){
            let _this = this;
            axios(
                {
                    method:"GET",
                    url: "http://localhost/doctors/all/",
                }
            ).then(function(resp){
                console.log(resp.data);
                _this.searchByCondition={}
                _this.doctors=resp.data.data;
            })
        }
      },



      mounted(){
        this.getAllDoctors()
    }
})